
goog.provide('esn.StrainedFriendSurvey');

goog.require('esn.Slider');
goog.require('esn.dao.StrainedFriendSurveyDao');
goog.require('esn.soy.strainedFriendSurveyList');
goog.require('goog.array');
goog.require('goog.dom.classes');
goog.require('goog.style');
goog.require('goog.ui.Component');
goog.require('goog.ui.CustomButton');


/**
 * @constructor
 */
esn.StrainedFriendSurvey = function(appContext, surveyModel, opt_domHelper) {
  goog.base(this, opt_domHelper);

  this.surveyDao_ = new esn.dao.StrainedFriendSurveyDao(
      surveyModel,
      appContext.getService(esn.constants.ServiceId.SURVEY_CLIENT),
      2000);

  this.setId(String(surveyModel.getFriend().getId()));
  this.setModel(surveyModel);
};
goog.inherits(esn.StrainedFriendSurvey, goog.ui.Component);

esn.StrainedFriendSurvey.EventType = {
  REMOVE_CLICK: goog.events.getUniqueId('remove_click')
};

esn.StrainedFriendSurvey.RELATIONSHIP_VALUES = [
  'Slight dislike',
  'Dislike',
  'Strong dislike',
  'Hatred'
];

esn.StrainedFriendSurvey.prototype.relationshipLevelSlider_;

esn.StrainedFriendSurvey.prototype.removeButton_;

esn.StrainedFriendSurvey.prototype.getDao = function() {
  return this.surveyDao_;
};

esn.StrainedFriendSurvey.prototype.createDom = function() {  
  var dom = this.getDomHelper();
  var tableEl = dom.createElement('table');
  tableEl.innerHTML = esn.soy.strainedFriendSurveyList.row(
      {'Friend': this.getModel().getFriend().toObject()});
  var rowEl = tableEl.getElementsByTagName('tr')[0];
  this.setElementInternal(rowEl);

  this.removeButton_ = new goog.ui.CustomButton('Remove');
  this.addChild(this.removeButton_);
  this.removeButton_.render(this.getElementByClass('removeButton'));

  this.relationshipLevelSlider_ = new esn.Slider(
      esn.StrainedFriendSurvey.RELATIONSHIP_VALUES,
      this.getElementByClass('relationshipLevelValue'));
  this.addChild(this.relationshipLevelSlider_);
  this.relationshipLevelSlider_.decorate(
      this.getElementByClass('relationshipLevelSlider'));
};

esn.StrainedFriendSurvey.prototype.enterDocument = function() {  
  goog.base(this, 'enterDocument');

  // Now that slider components are visible set their default values.
  this.relationshipLevelSlider_.setValue(
      this.getModel().getRelationshipLevel());

  this.getHandler().listen(
      this,
      esn.Slider.EventType.CHANGE,
      this.handleSliderChange_);

  this.getHandler().listen(
      this.removeButton_,
      goog.ui.Component.EventType.ACTION,
      function(e) {
        this.dispatchEvent(esn.StrainedFriendSurvey.EventType.REMOVE_CLICK);
      });
};

esn.StrainedFriendSurvey.prototype.validateCompletion = function() {
  if (!goog.isDef(this.getModel().getRelationshipLevel())) {
    var cellEl = this.relationshipLevelSlider_.getElement().parentNode;
    goog.dom.classes.add(cellEl, 'missing');
    return false;
  }
  return true;
};

esn.StrainedFriendSurvey.prototype.handleSliderChange_ = function(e) {
  if (e.target instanceof esn.Slider) {
    var slider = e.target;
    if (slider == this.relationshipLevelSlider_) {
      this.getModel().setRelationshipLevel(
          this.relationshipLevelSlider_.getValue());      
    }
    // Save changes.
    this.surveyDao_.save();    
  }
};
